package org.springblade.modules.restaurant.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.annotation.ApiLog;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.modules.restaurant.entity.UserSalary;
import org.springblade.modules.restaurant.entity.UserSalaryDay;
import org.springblade.modules.restaurant.service.IUserSalaryDayService;
import org.springblade.modules.restaurant.service.IUserSalaryService;
import org.springblade.modules.restaurant.vo.UserSalaryDayVO;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @author harry
 * @公众号 Harry技术
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/api/restaurant/usersalaryday")
@Tag(name = "API - 考勤管理")
public class ApiUserSalaryDayController {
	private IUserSalaryDayService userSalaryDayService;
	private IUserSalaryService userSalaryService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@Operation(summary = "详情", description = "传入userSalaryDay")
	public R<UserSalaryDayVO> detail(UserSalaryDay userSalaryDay) {
		UserSalaryDay detail = userSalaryDayService.getById(userSalaryDay.getId());
		if (detail != null) {
			UserSalaryDayVO userSalaryDayVO = BeanUtil.toBean(detail, UserSalaryDayVO.class);
			UserSalary userSalary = userSalaryService.getOne(Wrappers.<UserSalary>lambdaQuery()
				.eq(UserSalary::getUserId, detail.getUserId())
			);
			if (userSalary != null) {
				userSalaryDayVO.setRestDays(userSalary.getRestDays());
			}
			return R.data(userSalaryDayVO);
		}
		return R.fail("");
	}

	/**
	 * 分页 用户日薪表
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@Operation(summary = "分页", description = "传入userSalaryDay")
	public R<IPage<UserSalaryDay>> list(UserSalaryDayVO userSalaryDay, Query query) {
		String startTime = userSalaryDay.getStartTime();
		String endTime = userSalaryDay.getEndTime();

		LambdaQueryWrapper<UserSalaryDay> wrapper = new LambdaQueryWrapper<>();

		if (StrUtil.isNotBlank(startTime) && StrUtil.isNotBlank(endTime)) {
			startTime = DateUtil.formatDate(DateUtil.parse(startTime, DatePattern.NORM_DATE_PATTERN));
			endTime = DateUtil.formatDate(DateUtil.parse(endTime, DatePattern.NORM_DATE_PATTERN));
		} else {
			Date now = DateUtil.date();
			// 获取今天开始时间
			startTime = DateUtil.formatDate(DateUtil.beginOfDay(now));
			// 获取当前时间
			endTime = DateUtil.formatDate(now);
		}
		wrapper.eq(ObjUtil.isNotNull(userSalaryDay.getCreateDept()), UserSalaryDay::getCreateDept, userSalaryDay.getCreateDept());
		wrapper.between(UserSalaryDay::getTime, startTime, endTime);
		wrapper.like(StrUtil.isNotBlank(userSalaryDay.getUserName()), UserSalaryDay::getUserName, userSalaryDay.getUserName());
		wrapper.orderByDesc(UserSalaryDay::getTime);

		IPage<UserSalaryDay> pages = userSalaryDayService.page(Condition.getPage(query), wrapper);
		return R.data(pages);
	}

	/**
	 * 新增或修改 用户日薪表
	 */
	@ApiLog(value = "点到状态更新")
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@Operation(summary = "新增或修改", description = "传入userSalaryDay")
	public R submit(@Valid @RequestBody UserSalaryDay userSalaryDay) {
		UserSalaryDay detail = userSalaryDayService.getById(userSalaryDay.getId());
		Integer type = userSalaryDay.getType();
		// 如果 type 为  2  7 则修改用户薪资表
		if (detail != null && (type == 2 || type == 7)) {
			userSalaryService.updateRestDays(detail.getUserId(), type);
		}
		return R.status(userSalaryDayService.saveOrUpdate(userSalaryDay));
	}

}
